package sdk.webview.fmc.com.fmcsdk.check.persenter;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.UserApi;
import sdk.webview.fmc.com.fmcsdk.bean.UploadImage;
import sdk.webview.fmc.com.fmcsdk.bean.UploadVideoBean;
import sdk.webview.fmc.com.fmcsdk.check.interfaces.WaterMeterView;
import sdk.webview.fmc.com.fmcsdk.util.DateUtil;
import sdk.webview.fmc.com.fmcsdk.util.ImageCompressor;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;
import top.zibin.luban.OnCompressListener;

public class WaterMeterPresenter extends BasePresenter<WaterMeterView> {

    public void updateInspectionPointWorkRequest(String picId, String url, String shrinkUrl, String ownerId, String locationUrl, String description, boolean isPic) {
        String ownerObject = "insp_order_chklist";
        String ownerType = "chklist";
        String storePath = "/filestorage/"+ DateUtil.getCurrentDate("yyyyMMdd")+"/"+picId+".jpg";
        String thumbImagePath = "/filestorage/"+ DateUtil.getCurrentDate("yyyyMMdd")+"/thumb_"+picId+".jpg";
        String offlineDate = DateUtil.getCurrentDate("yyyy/MM/dd+HH:mm:ss");
        view.showLoading();
        getApi(UserApi.class).updateInspectionPointWorkRequest(getDefaultAddress() + Url.UPLOAD_FILES_SERVICE, Url.XMLHTTPREQUEST,
                        UserHelper.getToken(),UserHelper.getToken(), picId , ownerId, ownerObject, ownerType,storePath,"jpg",thumbImagePath,"IMAGE",offlineDate, description)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        view.updateInspectionPointWorkRequestSuccess(picId, url, shrinkUrl,isPic);
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.updateInspectionPointWorkRequestFail(picId, url, shrinkUrl,locationUrl,isPic);
                        view.hideLoading();
                    }

                    @Override
                    public void onRequestAgain() {
                        updateInspectionPointWorkRequest(picId,url,shrinkUrl,ownerId,locationUrl,description,isPic);
                    }
                });
    }


    /**
     * 上传图片到客户端
     */
    public void uploadImgForClient(String url,String locationUrl, String id) {
        File files = new File(url);
        if (!files.exists()) {
            files = new File(url);
            if (!files.exists()) {
                return;
            }
        }
        new ImageCompressor(view.getViewContext()).compressImageInPlace(url, new OnCompressListener() {
            @Override
            public void onStart() {
                view.showLoading();
            }

            @Override
            public void onSuccess(File file) {
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);//,System.currentTimeMillis()+""
                RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis() + "");//,+""
                getApi(UserApi.class).uploadImgForClient(getDefaultAddress() + Url.UPLOAD_IMG, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody, timeBody)
                        .compose(new ResponseTransformer<>(bindToLifeCycle(1)))
                        .subscribe(new ResponseSubscriber<UploadImage>(view) {
                            @Override
                            public void success(UploadImage baseData) {
                                //图片上传到服务器
                                view.uploadImgForClientSuccess(baseData,locationUrl);
                                //uploadFilesService(baseData,ids,url,ownerId);
                                view.hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                view.uploadImgForClientOnError(url,locationUrl, id);
                                //view.uploadImgForClientOnError(url, ids);
                                view.hideLoading();
                            }


                            @Override
                            public void onRequestAgain() {
                                uploadImgForClient(url,locationUrl,id);
                            }
                        });


            }

            @Override
            public void onError(Throwable e) {
                //view.uploadImgForClientOnError(url, ids);
                view.uploadImgForClientOnError(url, locationUrl, id);
            }
        });


    }
    public void deletePic(String id, int groupPosition, int position) {
        view.showLoading();
        getApi(UserApi.class).deletePic(getDefaultAddress() + Url.POST_DELETE_PIC, UserHelper.getToken(), id)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>() {
                    @Override
                    public void success(BaseData baseData) {
                        //view.deletePicSuccess(groupPosition, position);
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.hideLoading();
                    }

                    @Override
                    public void onRequestAgain() {
                    }


                });
    }

    public void uploadVideo(final String localUrl, final String ids, File files) {
//        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
        RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

        RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), ids);
        getApi(UserApi.class).uploadVideo(getDefaultAddress() + Url.UPLOAD_VIDEO, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody,timeBody)
                .compose(new ResponseTransformer<>(this.<UploadVideoBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<UploadVideoBean>(view) {
                    @Override
                    public void success(UploadVideoBean baseData) {
                        view.uploadVideoSuccess(baseData.getRecord().getUrl(), localUrl, baseData.getRecord().getId());

                       // view.uploadImgForClientSuccess(baseData,localUrl);
                        // view.uploadImgOrVideoEnd();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                       // view.uploadVideoError(localUrl, ids);
                    }


                    @Override
                    public void onRequestAgain() {

                    }
                });
    }


}
